package com.shizhanzhe.gxb.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.WXLoginActivity;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.home.adapter.MallGoodsAdapter;
import com.shizhanzhe.gxb.home.bean.GoodsListBean;
import com.shizhanzhe.gxb.home.bean.Mallgoods;
import com.shizhanzhe.gxb.home.bean.MallgoodsPage;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by YJ on 2019/1/17.
 */
public class MallGoodsActivity extends SwipeBackActivity {

    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.no_data)
    ImageView noData;
    private MallGoodsAdapter mAdapter;
    private int page = 0;
    private int mall_id = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall_goods);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        topTitle.setText(getIntent().getStringExtra("name"));
        mall_id = getIntent().getIntExtra("mall_id", 0);
        Log.d("____", mall_id + "");
        initData();
        setListener();
    }

    @OnClick(R.id.backarea)
    public void onViewClicked() {
        finish();
    }

    private void initData() {
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MallGoodsAdapter(MallGoodsActivity.this);
        recycler.setAdapter(mAdapter);
        mallgoods();
    }

    private void setListener() {
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 0;
                mallgoods();
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mallgoods();
            }
        });

        mAdapter.setListener(new MallGoodsAdapter.MallGoodsListener() {
            @Override
            public void click(GoodsListBean item) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(MallGoodsActivity.this, PDDDetailActivity.class).putExtra("goods_id", item.getGoods_id()));
                } else {
                    startActivity(new Intent(MallGoodsActivity.this, WXLoginActivity.class));
                }
            }
        });
    }

    private void mallgoods() {
        LoadingDialog.showDialogForLoading(this);
        String url = "";
        if (page == 0) {
            url = Path.merchantlist() + "?mall_id=" + mall_id;
        } else {
            url = Path.mallgoods(mall_id, page);
        }
        Log.d("______", url);
        OkHttpDownloadJsonUtil.downloadJson(MallGoodsActivity.this, url, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                LoadingDialog.cancelDialogForLoading();
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    if (page == 0) {
                        Mallgoods mallgoods = gson.fromJson(json, Mallgoods.class);
                        if (mallgoods.getData().getMerchant_list_response().getMall_search_info_vo_list().get(0).getGoods_detail_vo_list().size() > 0) {
                            mAdapter.updateData(page, mallgoods.getData().getMerchant_list_response().getMall_search_info_vo_list().get(0).getGoods_detail_vo_list());
                        }
                    } else {
                        MallgoodsPage mallgoods = gson.fromJson(json, MallgoodsPage.class);
                        if (mallgoods.getData().getGoods_info_list_response().getGoods_list().size() > 0) {
                            mAdapter.updateData(page, mallgoods.getData().getGoods_info_list_response().getGoods_list());
                        } else {
                            Toast.makeText(MallGoodsActivity.this, "不能再有更多了", Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    Toast.makeText(MallGoodsActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
